<template>
  <div class="wallet">
    <h2>简化版 DGD 钱包</h2>

    <!-- 创建钱包 -->
    <div>
      <button @click="createAccount">创建新钱包</button>
    </div>

    <!-- 登录钱包（私钥导入） -->
    <div style="margin-top:15px">
      <input v-model="importKey" placeholder="输入私钥登录" />
      <button @click="loginWallet">登录钱包</button>
    </div>

    <!-- 钱包信息 -->
    <div v-if="address" style="margin-top:20px">
      <p><strong>地址：</strong>{{ address }}</p >
      <p><strong>私钥：</strong>{{ privateKey }}</p >
      <p><strong>ETH 余额：</strong>{{ ethBalance }} ETH</p >
      <p><strong>DGD 余额：</strong>{{ dgdBalance }} DGD</p >
    </div>

    <!-- 模拟充值 -->
    <div v-if="address" style="margin-top:10px">
      <input v-model.number="depositAmount" placeholder="充值 DGD 数量" />
      <button @click="depositDGD">充值 DGD</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ethers } from "ethers";

const address = ref("");
const privateKey = ref("");
const ethBalance = ref(0);
const dgdBalance = ref(0);

const depositAmount = ref(0);
const importKey = ref("");

// 创建新钱包
function createAccount() {
  const wallet = ethers.Wallet.createRandom();
  loadWallet(wallet);
}

// 登录钱包（私钥导入）
function loginWallet() {
  try {
    const wallet = new ethers.Wallet(importKey.value.trim());
    loadWallet(wallet);
  } catch (e) {
    alert("私钥格式错误");
  }
}

// 把钱包加载到前端
function loadWallet(wallet) {
  address.value = wallet.address;
  privateKey.value = wallet.privateKey;

  // 模拟余额（如果你有后端也可以从后端拉）
  ethBalance.value = 10;
  dgdBalance.value = 100;
}

// 模拟充值
function depositDGD() {
  if (!depositAmount.value) return;
  dgdBalance.value += depositAmount.value;
  depositAmount.value = 0;
}
</script>

<style scoped>
.wallet { max-width:400px; margin:20px auto; padding:10px; border:1px solid #ccc; border-radius:6px; }
input { padding:4px 6px; margin-right:6px; width:180px; }
button { padding:4px 10px; cursor:pointer; }
</style>